<?php

################################################################################################################
Function test_connection($server, $user, $pass)
{
  if (!($connection = @mysql_connect($server, $user, $pass)))
		return (-1);
	mysql_close($connection);
  return (0);
}

################################################################################################################
Function create_base($server, $user, $pass, $database, $prefixe, $auth)
{
	/*
		-1 No connection
		-2 SQL
		-3 No config file
	*/
	
	$pre = trim($prefixe);
	$database = trim($database);
	$user = trim($user);
	$pass = trim($pass);
	
	if (!($connection = @mysql_connect($server, $user, $pass)))
		return (-1);
	@mysql_query("CREATE DATABASE IF NOT EXISTS $database", $connection);
	
	$table_users_auth = $pre."users_auth";
	$table_users_infos = $pre."users_infos";
	
	$table_templates = $pre."templates";
	$table_modules = $pre."modules";
	$table_modules_rights = $pre."modules_rights";
	$table_admin = $pre."admin";
	$table_groups = $pre."groups";
	$table_users_groups = $pre."users_groups";
	$table_users = $pre."users";
	$table_history_users_notes = $pre."history_users_notes";
	$table_history_users = $pre."history_users";
	$table_history_type = $pre."history_type";
	
	
	$req[0] = "CREATE TABLE $table_users_auth (`uid` int(11) unsigned NOT NULL auto_increment,`login` varchar(20) NOT NULL,`password` varchar(32) NOT NULL,`mail` varchar(254) NOT NULL,`tid` tinyint(4) unsigned NOT NULL,PRIMARY KEY  (`uid`),UNIQUE KEY `uid` (`uid`),UNIQUE KEY `login` (`login`), UNIQUE KEY `mail` (`mail`)) ENGINE=MyISAM DEFAULT CHARSET=utf8";	
	$req[1] = "CREATE TABLE $table_users_infos (`uid` int(11) unsigned NOT NULL,`last_ip` varchar(15) NOT NULL,`last_login` datetime NOT NULL,`valid` enum('ok','ko') NOT NULL default 'ok',PRIMARY KEY  (`uid`),UNIQUE KEY `uid` (`uid`)) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8";
	$req[2] = "CREATE TABLE $table_templates (`tid` tinyint(4) unsigned NOT NULL auto_increment,`name` varchar(20) NOT NULL,`path` varchar(254) NOT NULL,`type` enum('system','user') NOT NULL default 'user',`version` varchar(10) NOT NULL default '0.0.1',`default` enum('ok','ko') NOT NULL default 'ko',PRIMARY KEY  (`tid`),UNIQUE KEY `tid` (`tid`),UNIQUE KEY `path` (`path`)) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;";
	$req[3] = "CREATE TABLE $table_modules (`mid` tinyint(4) unsigned NOT NULL auto_increment,`name` varchar(254) NOT NULL,`path` varchar(255) NOT NULL,`actif` enum('ok','ko') NOT NULL default 'ko',`name_a` varchar(254) default NULL,`name_l` varchar(254) default NULL,`show_a` enum('ko','ok') NOT NULL default 'ko',`show_l` enum('ko','ok') NOT NULL default 'ko',`link_a` varchar(254) default NULL,`link_l` varchar(254) default NULL,`place` tinyint(2) default NULL,`type` enum('system','user','link') NOT NULL,`depend` varchar(254) default NULL,`description` text,`version` varchar(10) NOT NULL default '0.0.1',PRIMARY KEY  (`mid`),UNIQUE KEY `mid` (`mid`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8";
	$req[4] = "CREATE TABLE $table_modules_rights (`rid` int(11) unsigned NOT NULL auto_increment,`mid` int(11) unsigned NOT NULL,`type` enum('uid','gid') NOT NULL,`id` int(11) unsigned NOT NULL,PRIMARY KEY  (`rid`),UNIQUE KEY `rid` (`rid`)) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8";
	$req[5] = "CREATE TABLE $table_admin (`aid` tinyint(1) unsigned NOT NULL auto_increment,`admin` varchar(50) NOT NULL,`password` varchar(32) NOT NULL,`mail` varchar(255) NOT NULL,PRIMARY KEY  (`aid`),UNIQUE KEY `aid` (`aid`),UNIQUE KEY `admin` (`admin`),UNIQUE KEY `password` (`password`),UNIQUE KEY `mail` (`mail`)) ENGINE=MyISAM DEFAULT CHARSET=utf8";
	$req[6] = "CREATE TABLE $table_groups (`gid` int(11) unsigned NOT NULL auto_increment,`title` varchar(50) NOT NULL,`description` text,`view` enum('all','request') NOT NULL default 'all',`manager` int(11) unsigned NOT NULL default '1',PRIMARY KEY  (`gid`),UNIQUE KEY `gid` (`gid`),UNIQUE KEY `title` (`title`)) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8";
	$req[7] = "CREATE TABLE $table_users_groups (`id` int(11) unsigned NOT NULL auto_increment,`uid` int(11) unsigned NOT NULL,`gid` int(11) unsigned NOT NULL,`status` enum('ok','wait') NOT NULL default 'wait',PRIMARY KEY  (`id`),UNIQUE KEY `id` (`id`)) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8";
	$req[8] = "CREATE TABLE $table_users  (`uid` int(11) unsigned NOT NULL,`first` varchar(100) default NULL,`last` varchar(100) default NULL,`birth` date default NULL,PRIMARY KEY  (`uid`),UNIQUE KEY `uid` (`uid`)) ENGINE=MyISAM DEFAULT CHARSET=utf8";
	$req[9] = "CREATE TABLE $table_history_users_notes (`nid` int(11) unsigned NOT NULL auto_increment,`dest` int(11) unsigned NOT NULL,`uid` int(11) unsigned NOT NULL,`type` tinyint(3) unsigned NOT NULL,`link` varchar(254) default NULL,`value` varchar(254) default NULL,`date` datetime NOT NULL,`status` enum('ok','ko') NOT NULL default 'ko',PRIMARY KEY  (`nid`),UNIQUE KEY `hid` (`nid`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
	$req[10] = "CREATE TABLE $table_history_users (`hid` int(11) unsigned NOT NULL auto_increment,`uid` int(11) unsigned NOT NULL,`type` tinyint(3) unsigned NOT NULL,`link` varchar(254) default NULL,`value` varchar(254) default NULL,`date` datetime NOT NULL,PRIMARY KEY  (`hid`),UNIQUE KEY `hid` (`hid`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
	$req[11] = "CREATE TABLE $table_history_type (`tid` int(11) unsigned NOT NULL auto_increment,`value` varchar(254) NOT NULL,`ico` varchar(254) NOT NULL default 'default.png',PRIMARY KEY  (`tid`),UNIQUE KEY `tid` (`tid`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
	
	$nb_req = count($req);
	$i = 0;
	while ($i < $nb_req)
	{
		if (!(@mysql_db_query($database, $req[$i], $connection)))
		{
			mysql_close($connection);
			return (-2);
		}
		$i++;
	}
	
	$req = "";
	$user_mysql = create_id(15);
	$pass_mysql = create_id(15);
	$ip = $_SERVER['REMOTE_ADDR'];
	$date = date('Y-m-d H:i:s');
	$bdate = date('Y-m-d');
	
	$req[0] = "INSERT INTO $table_admin VALUES (1, \"$_SESSION[infos_admin]\", md5(\"$_SESSION[infos_passadmin]\"), \"$_SESSION[infos_mailadmin]\")";
	$req[1] = "INSERT INTO $table_users_infos VALUES (1, \"$ip\", \"$date\", \"ok\")";
	
	if ($auth == "mysql")
	{
		$req[2] = "INSERT INTO $table_users_auth VALUES (1, \"$_SESSION[infos_admin]\", md5(\"$_SESSION[infos_passadmin]\"), \"$_SESSION[infos_mailadmin]\", \"1\")";
		$req[3] = "INSERT INTO $table_modules (`mid`, `name`, `path`, `actif`, `name_a`, `name_l`, `show_a`, `show_l`, `link_a`, `link_l`, `place`, `type`, `depend`, `description`, `version`) VALUES(1, 'Accueil', 'index', 'ok', 'Accueil', 'Accueil', 'ok', 'ok', 'index.php', 'index.php', 1, 'system', \"2\", \"Module permettant l'affichage de la page d'acceuil\", \"1.1.0\"), (2, 'Identification', 'login', 'ok', NULL, NULL, 'ko', 'ko', NULL, NULL, 0, 'system', NULL, \"Module permettant la gestion de l'identification des utilisateurs sur le site.\", \"1.1.0\"), (3, 'Inscription', 'signup', 'ok', 'Inscription', NULL, 'ok', 'ko', 'signup.php', NULL, 2, 'system', \"1\", \"Module permettant aux visiteur du site de cr&eacute;er un compte.\", \"1.1.0\"), (4, 'Profil', 'profil', 'ok', NULL, 'Mon compte', 'ko', 'ok', NULL, 'profil.php', 2, 'system', \"2\", \"Module permettant aux utilisateurs du site de g&eacute;rer leur compte.\", \"1.1.0\")";
		$req[4] = "CREATE USER '$user_mysql'@'%' IDENTIFIED BY '$pass_mysql'";
		$req[5] = "GRANT SELECT ,INSERT ,UPDATE ,DELETE ON $database . * TO '$user_mysql'@'%' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0";
		$req[11] = "INSERT INTO $table_users VALUES (1, '$_SESSION[infos_admin]', '', '$bdate')";
	}
	elseif (($auth == "LDAP") || ($auth == "WLS"))
	{
		$req[2] = "INSERT INTO $table_users_auth VALUES (1, \"$_SESSION[infos_admin]\", md5(\"no_pass\"), \"$_SESSION[infos_mailadmin]\", \"1\")";
		$req[3] = "INSERT INTO $table_modules (`mid`, `name`, `path`, `actif`, `name_a`, `name_l`, `show_a`, `show_l`, `link_a`, `link_l`, `place`, `type`, `depend`, `description`, `version`) VALUES(1, 'Accueil', 'index', 'ok', 'Accueil', 'Accueil', 'ok', 'ok', 'index.php', 'index.php', 1, 'system', \"2\", \"Module permettant l'affichage de la page d'acceuil\", \"1.1.0\"), (2, 'Identification', 'login', 'ok', 'Identification', NULL, 'ok', 'ko', 'login.php', NULL, 2, 'system', NULL, \"Module permettant la gestion de l'identification des utilisateurs sur le site.\", \"1.1.0\"), (3, 'Inscription', 'signup', 'ko', NULL, NULL, 'ko', 'ko', NULL, NULL, 2, 'system', \"1\", \"Module permettant aux visiteur du site de cr&eacute;er un compte.\", \"1.1.0\"), (4, 'Profil', 'profil', 'ok', NULL, 'Mon compte', 'ko', 'ok', NULL, 'profil.php', 2, 'system', \"2\", \"Module permettant aux utilisateurs du site de g&eacute;rer leur compte.\", \"1.1.0\")";
		$req[4] = "CREATE USER '$user_mysql'@'%' IDENTIFIED BY '$pass_mysql'";
		$req[5] = "GRANT SELECT ,INSERT ,UPDATE ,DELETE ON $database . * TO '$user_mysql'@'%' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0";
	}
	
	$req[6] = "INSERT INTO $table_templates VALUES (1, \"D&eacute;faut\", \"default\", \"system\", \"1.0.0\", \"ok\")";
	$req[7] = "INSERT INTO $table_templates VALUES (2, \"Green & Gray\", \"green_gray\", \"system\", \"1.1.1\", \"ko\")";
	$req[8] = "INSERT INTO $table_groups VALUES(1, 'Administrateurs', 'Groupe des administrateurs du site.', 'request', 1),(2, 'Utilisateurs', 'Groupe de tous les utilisateurs du site.', 'all', 1)";
	$req[9] = "INSERT INTO $table_users_groups VALUES(1, 1, 1, 'ok'),(2, 1, 2, 'ok')";
	$req[10] = "INSERT INTO $table_modules_rights (`rid`, `mid`, `type`, `id`) VALUES (2, 4, 'gid', 2)";
	
	$nb_req = count($req);
	$i = 0;
	while ($i < $nb_req)
	{
		if (!(@mysql_db_query($database, $req[$i], $connection)))
		{
			if (($i != 4) && ($i != 5))
			{
				mysql_close($connection);
				return (-2);
			}
			else
			{
				$user_mysql = $user;
				$pass_mysql = $pass;
			}
		}
		$i++;
	}
	mysql_close($connection);
	
	$get_result = create_config_mysql($server, $user_mysql, $pass_mysql, $database, $pre);
	if ($get_result == -1)
		return (-3);
	return (0);
}
?>